#include "mcmcBottomupCue.h"
#include "mcmcImageFilters.h"
#include "ItgImage.h"


/////////////////////////////////////////////
/////////////////////////////////////////////

// construction & destruction
MCMC_CueAtPixel::MCMC_CueAtPixel()
{
	p_cues		= NULL;
	num_cues	= 0;
}

MCMC_CueAtPixel::~MCMC_CueAtPixel()
{
	Free();
}

void MCMC_CueAtPixel::Free(void)
{
	num_cues	= 0;

	if (p_cues != NULL)
	{
		for (int i=0; i<num_cues; i++)
			if (p_cues[i] != NULL)
				delete p_cues[i];
		delete []p_cues;
		p_cues = NULL;
	}
}

void MCMC_CueAtPixel::InitAsLevelOne(void)
{
	Free();

	// 3 types of cue
	p_cues = new PMCMC_BtmupCue[4];

	p_cues[0] = new MCMC_CueLocalGaussian;
	p_cues[1] = new MCMC_CueEdge;
	p_cues[2] = new MCMC_CueFace;

	num_cues = 3;
}


/////////////////////////////////////////////
/////////////////////////////////////////////

// construction & destruction
MCMC_ImageBtmupCues::MCMC_ImageBtmupCues()
{
}

MCMC_ImageBtmupCues::~MCMC_ImageBtmupCues()
{
}
/**/
void MCMC_ImageBtmupCues::Set(MCMC_FilterResp &filter_resp)
{
	mx_cue_pixel.SetDimension(filter_resp.rows(), filter_resp.cols());

	// obtain various local cues
	int						i,j,i1,i2,j1,j2;
	MCMC_CueLocalGaussian	*pcue_gaussain;
	MCMC_CueEdge			*pcue_edge;

	for (j=0; j<filter_resp.rows(); j++)
		for (i=0; i<filter_resp.cols(); i++)
		{
			// cues
			mx_cue_pixel(j, i).InitAsLevelOne();

			// local gaussian
			pcue_gaussain = (MCMC_CueLocalGaussian*) mx_cue_pixel(j, i).GetCue(0);

			// local mean and variance
			j1 = max(0, j-4);	i1 = max(0, i-4);
			j2 = j1+8;			i2 = i1+8;
			
			if (j2 >= filter_resp.rows())
			{
				j2 = filter_resp.rows()-1;
				j1 = j2-8;
			}
			if (i2 >= filter_resp.cols())
			{
				i2 = filter_resp.cols()-1;
				i1 = i2-8;
			}

			pcue_gaussain->mean = (float)filter_resp.itg_image.RectMean(i1, j1, i2, j2);
			pcue_gaussain->var	= (float)filter_resp.itg_image.RectVar(i1, j1, i2, j2);

			// edge cue
			pcue_edge = (MCMC_CueEdge*)mx_cue_pixel(j, i).GetCue(1);
			pcue_edge->g = (float)filter_resp.ras_edge_g(j, i);
			pcue_edge->theta = (float)filter_resp.ras_edge_theta(j, i);
		}
}


